package com.example.xmhds_1.ws;

import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.example.xmhds_1.entity.UserAccount;
import com.example.xmhds_1.entity.UserInfo;
import com.example.xmhds_1.service.UserAccountService;
import com.example.xmhds_1.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 用户相关WebSocket处理类
 * 提供用户相关的WebSocket消息处理方法
 */
@Slf4j
@Component
public class UserWebSocket {
    /**
     * 用户服务
     */
    public static UserAccountService userAccountService;

    /**
     * 处理WebSocket登录请求
     *
     * 请求格式：
     * {
     *   "type": "login",
     *   "username": "用户名",
     *   "password": "密码"
     * }
     *
     * 响应格式：
     * 成功响应：
     * {
     *   "type": "login",
     *   "success": true,
     *   "message": "登录成功",
     *   "data": {
     *     "id": "用户ID",
     *     "username": "用户名",
     *     "email": "邮箱",
     *     "phone": "手机号",
     *     "status": "状态"
     *   },
     *   "token": "eyJhbGciOiJIUzI1NiJ9..."  // JWT token
     * }
     *
     * 失败响应：
     * {
     *   "type": "login",
     *   "success": false,
     *   "message": "用户名或密码错误"
     * }
     *
     * @param request WebSocket请求消息
     * @param response WebSocket响应消息
     * @return 处理后的响应消息
     */
    public JSONObject handleLogin(JSONObject request, JSONObject response) {
        String username = request.getString("username");
        String password = request.getString("password");
        // 参数校验
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            response.put("success", false);
            response.put("message", "登入失败");
            return response;
        }

        //log.info("处理登录请求: username={}", username);

        UserAccount user = userAccountService.login(username, password);

        if (user != null) {
            // 生成token
            String token = JwtUtil.generateToken(user);

            // 构建响应
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("data", user);
            response.put("token", token);

            log.info("用户登录成功: {}", username);
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
            log.warn("用户登录失败: {}", username);
        }
        return response;
    }

    /**
     * 处理注册请求
     *
     * 请求格式：
     * {
     *   "type": "register",
     *   "username": "用户名",
     *   "password": "密码"
     * }
     *
     * 响应格式：
     * 成功响应：
     * {
     *   "type": "register",
     *   "success": true,
     *   "message": "注册成功",
     *   "data": {
     *     "id": "用户ID",
     *     "username": "用户名"
     *   }
     * }
     *
     * 失败响应：
     * {
     *   "type": "register",
     *   "success": false,
     *   "message": "用户名已存在"
     * }
     */
    public JSONObject handleRegister(JSONObject request, JSONObject response) {
        String username = request.getString("username");
        String password = request.getString("password");

        //log.info("处理注册请求: username={}", username);

        UserAccount user = userAccountService.register(username, password);

        if (user != null) {
            response.put("success", true);
            response.put("message", "注册成功");
            response.put("data", user);
            log.info("用户注册成功: {}", username);
        } else {
            response.put("success", false);
            response.put("message", "用户名已存在");
            log.warn("用户注册失败: {}", username);
        }
        return response;
    }
}
